package com.demoku.web.web.demoAdmin;

import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iff.infra.util.JsonHelper;
import org.iff.infra.util.mybatis.plugin.Page;
import org.iff.infra.util.mybatis.plugin.PageVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.demoku.web.application.demoAdmin.AppTypeApplication;
import com.demoku.web.application.demoAdmin.vo.AppTypeVO;
import com.demoku.web.web.common.BaseController;

@Controller
public class AppTypeController extends BaseController {

	@Inject
	AppTypeApplication appTypeApplication;

	@RequestMapping("/index.do")
	public String index() {
		return "index";
	}
	
	@RequestMapping("/toAddAppType.do")
	public String toAddAppType(HttpServletRequest request, HttpServletResponse response) {
		return "demoAdmin/appTypeAdmin/addAppType";
	}

	@RequestMapping("/addAppType.do")
	public void addAppType(AppTypeVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		appTypeApplication.saveAppType(vo);
		outPrint(response, "ok");
	}
	
	@RequestMapping("/deleteAppType.do")
	public void deleteAppType(String id, AppTypeVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		String[] allId = id.split(",");
		for(String oneId : allId) {
			appTypeApplication.removeAppType(id);
		}
		outPrint(response, "ok");
	}
	
	@RequestMapping("/toEditAppType.do")
	public String toEditAppType(String id, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) {
		modelMap.addAttribute("appType", appTypeApplication.getAppTypeVO(id));
		return "demoAdmin/appTypeAdmin/editAppType";
	}
	
	@RequestMapping("/editAppType.do")
	public void editAppType(AppTypeVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		appTypeApplication.saveAppType(vo);
		outPrint(response, "ok");
	}

	@RequestMapping("/pageFindAllAppType4vo.do")
	public String pageFindAllAppType(AppTypeVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) {
		return "demoAdmin/appTypeAdmin/pageFindAllAppType";
	}
	
	@RequestMapping("/getAppType4Json.do")
	public void getAppType4Json(AppTypeVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		Page page = appTypeApplication.pageFindAllAppType(vo);
		outPrint(response, JsonHelper.toJson(new PageVo(page.getPageSize(), page.getTotalCount(), page.getCurrentPage(), page.getRows())));
	}
	
}
